home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue31 / bcolorbt / BCOLORBT.ZIP / ColorAEd.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-01-08  |  8.6 KB  |  314 lines

  1. {$I DFS.INC}  { Standard defines for all Delphi Free Stuff components }
  2.  
  3. {-----------------------------------------------------------------------------}
  4. { A Windows 95 and NT 4 style color selection button.  It displays a palette  }
  5. { of 20 color for fast selction and a button to bring up the color dialog.    }
  6. { Copyright 1996, Brad Stowers.  All Rights Reserved.                         }
  7. { This component can be freely used and distributed in commercial and private }
  8. { environments, provied this notice is not modified in any way and there is   }
  9. { no charge for it other than nomial handling fees.  Contact me directly for  }
  10. { modifications to this agreement.                                            }
  11. {-----------------------------------------------------------------------------}
  12. { Feel free to contact me if you have any questions, comments or suggestions  }
  13. { at bstowers@pobox.com.                                                      }
  14. { The lateset version will always be available on the web at:                 }
  15. {   http://www.pobox.com/~bstowers/delphi/                                    }
  16. {-----------------------------------------------------------------------------}
  17. { Date last modified:  December 27, 1997                                      }
  18. {-----------------------------------------------------------------------------}
  19.  
  20.  
  21. {-----------------------------------------------------------------------------}
  22. { TColorArrayEditor                                                           }
  23. {-----------------------------------------------------------------------------}
  24. { Description:                                                                }
  25. {   This is a support unit for the TDFSColorButton component (COLORBTN.PAS).  }
  26. {-----------------------------------------------------------------------------}
  27. unit ColorAEd;
  28.  
  29. interface
  30.  
  31. uses
  32.   WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  33.   Dialogs, CBtnForm, DsgnIntf, StdCtrls;
  34.  
  35. type
  36.   TColorArrayEditor = class(TForm)
  37.     btnOK: TButton;
  38.     btnCancel: TButton;
  39.     ColorDlg: TColorDialog;
  40.     procedure FormPaint(Sender: TObject);
  41.     procedure FormCreate(Sender: TObject);
  42.     procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  43.       Y: Integer);
  44.     procedure FormShow(Sender: TObject);
  45.     procedure FormClick(Sender: TObject);
  46.   private
  47.     FColors: TColorArrayClass;
  48.     FLastFrame: TPoint;
  49.     procedure SetColors(Val: TColorArrayClass);
  50.     procedure DrawSquare(X, Y: integer; AColor: TColor; IsFocused: boolean);
  51.     procedure FrameCurrentSquare;
  52.     function ValidColorIndex(X, Y: integer): boolean;
  53.     function GetCurrentSquare: TPoint;
  54.   public
  55.     constructor Create(AOwner: TComponent); override;
  56.     destructor Destroy; override;
  57.  
  58.     property Colors: TColorArrayClass
  59.        read FColors
  60.        write SetColors;
  61.   end;
  62.  
  63.   TColorArrayProperty = class(TClassProperty)
  64.   public
  65.     procedure Edit; override;
  66.     function GetAttributes: TPropertyAttributes; override;
  67.     function AllEqual: boolean; override;
  68.   end;
  69.  
  70. implementation
  71.  
  72. {$R *.DFM}
  73.  
  74.  
  75. constructor TColorArrayEditor.Create(AOwner: TComponent);
  76. begin
  77.   inherited Create(AOwner);
  78.   FColors := NIL;
  79. end;
  80.  
  81. destructor TColorArrayEditor.Destroy;
  82. begin
  83.   FColors.Free;
  84.   inherited Destroy;
  85. end;
  86.  
  87. procedure TColorArrayEditor.SetColors(Val: TColorArrayClass);
  88. begin
  89.   if FColors = NIL then
  90.     FColors := TColorArrayClass.Create(Val.XSize, Val.YSize);
  91.   FColors.Assign(Val);
  92. end;
  93.  
  94.  
  95.  
  96. procedure TColorArrayProperty.Edit;
  97. var
  98.   Dlg: TColorArrayEditor;
  99. begin
  100.   Application.CreateForm(TColorArrayEditor, Dlg);
  101.   try
  102.     Dlg.Caption := Self.GetName;
  103.     Dlg.Colors := TColorArrayClass(GetOrdValue);
  104.     if Dlg.ShowModal = mrOk then
  105.     begin
  106.       { SetOrdValue will operate on all selected propertiy values }
  107.       SetOrdValue(Longint(Dlg.Colors));
  108.       Modified;
  109.     end;
  110.   finally
  111.     Dlg.Free;
  112.   end;
  113. end;
  114.  
  115. function TColorArrayProperty.GetAttributes: TPropertyAttributes;
  116. begin
  117.   Result := [paDialog, paReadOnly, paMultiSelect];
  118. end;
  119.  
  120. function TColorArrayProperty.AllEqual: boolean;
  121. var
  122.   SourceColors: TColorArrayClass;
  123.   x: integer;
  124. begin
  125.   Result := FALSE;
  126.   if PropCount > 1 then
  127.   begin
  128.     { Get first selected color set }
  129.     SourceColors := TColorArrayClass(GetOrdValue);
  130.     for x := 1 to PropCount-1 do
  131.     begin
  132.       { Compare first selected to all other selected color sets }
  133.       if not SourceColors.IsEqualTo(TColorArrayClass(GetOrdValueAt(x))) then
  134.         exit;
  135.     end;
  136.   end;
  137.   Result := TRUE;
  138. end;
  139.  
  140.  
  141. procedure TColorArrayEditor.FormPaint(Sender: TObject);
  142. var
  143.   X, Y: integer;
  144. begin
  145.   for x := 1 to Colors.XSize do
  146.   begin
  147.     for y := 1 to Colors.YSize do
  148.     begin
  149.       { Draw color square }
  150.       DrawSquare(X, Y, FColors[x,y], FALSE);
  151.     end;
  152.   end;
  153.  
  154.   { Draw the current selection }
  155.   FrameCurrentSquare;
  156.  
  157.   { Draw seperator line }
  158.   y := Colors.YSize * 18 + 14;
  159.   with Canvas do
  160.   begin
  161.     Pen.Color := clBtnShadow;
  162.     MoveTo(5, y);
  163.     LineTo(ClientWidth - 5, y);
  164.     Pen.Color := clBtnHighlight;
  165.     inc(y);
  166.     MoveTo(5, y);
  167.     LineTo(ClientWidth - 5, y);
  168.   end;
  169.  
  170. end;
  171.  
  172. procedure TColorArrayEditor.DrawSquare(X, Y: integer; AColor: TColor; IsFocused: boolean);
  173. begin
  174.   if ValidColorIndex(X, Y) then
  175.   begin
  176.     X := (X-1) * 18 + 10;
  177.     Y := (Y-1) * 18 + 10;
  178.   end else
  179.     exit;
  180.  
  181.   with Canvas do
  182.   begin
  183.     if IsFocused then
  184.       Pen.Color := clBlack
  185.     else
  186.       Pen.Color := clBtnFace;
  187.     MoveTo(X-1,Y-1);
  188.     LineTo(X+16, Y-1);
  189.     LineTo(X+16, Y+16);
  190.     LineTo(X-1, Y+16);
  191.     LineTo(X-1, Y-1);
  192.  
  193.     if IsFocused then
  194.     begin
  195.       { Draw frame }
  196.       MoveTo(X+1, Y+1);
  197.       LineTo(X+14, Y+1);
  198.       LineTo(X+14, Y+14);
  199.       LineTo(X+1, Y+14);
  200.       LineTo(X+1, Y+1);
  201.       Pen.Color := clWhite;
  202.       MoveTo(X, Y);
  203.       LineTo(X+15, Y);
  204.       LineTo(X+15, Y+15);
  205.       LineTo(X, Y+15);
  206.       LineTo(X, Y);
  207.     end else begin
  208.       Pen.Color := clGray;
  209.       MoveTo(X, Y+15);
  210.       LineTo(X, Y);
  211.       LineTo(X+15, Y);
  212.       Pen.Color := clWhite;
  213.       LineTo(X+15, Y+15);
  214.       LineTo(X, Y+15);
  215.       Pen.Color := clBlack;
  216.       MoveTo(X+1, Y+14);
  217.       LineTo(X+1, Y+1);
  218.       LineTo(X+14, Y+1);
  219.       Pen.Color := RGB(223, 223, 223);
  220.       LineTo(X+14, Y+14);
  221.       LineTo(X+1, Y+14);
  222.     end;
  223.  
  224.     Brush.Color := AColor;
  225.     FillRect(Rect(X+2, Y+2, X+14, Y+14));
  226.   end;
  227. end;
  228.  
  229.  
  230. procedure TColorArrayEditor.FrameCurrentSquare;
  231.  
  232.   function ComparePoints(const Pt1, Pt2: TPoint): boolean;
  233.   begin
  234.     Result := ((Pt1.X = Pt2.X) and (Pt1.Y =Pt2.Y));
  235.   end;
  236.  
  237. var
  238.   NewFrame: TPoint;
  239. begin
  240.   NewFrame := GetCurrentSquare;
  241.   if not ComparePoints(NewFrame, FLastFrame) and
  242.      ValidColorIndex(NewFrame.X, NewFrame.Y) then
  243.   begin
  244.     { Unframe the last one }
  245.     if ValidColorIndex(FLastFrame.X, FLastFrame.Y) then
  246.       with FLastFrame do
  247.         DrawSquare(X, Y, FColors[X, Y], FALSE);
  248.     with NewFrame do
  249.       DrawSquare(X, Y, FColors[X, Y], TRUE);
  250.     FLastFrame := NewFrame;
  251.   end;
  252. end;
  253.  
  254. function TColorArrayEditor.ValidColorIndex(X, Y: integer): boolean;
  255. begin
  256.   Result := ((X > 0) and (X <= Colors.XSize) and
  257.      (Y > 0) and (Y <= Colors.YSize));
  258. end;
  259.  
  260. function TColorArrayEditor.GetCurrentSquare: TPoint;
  261. var
  262.   CurPos: TPoint;
  263. begin
  264.   GetCursorPos(CurPos);
  265.   CurPos := ScreenToClient(CurPos);
  266.   Result := Point(((CurPos.X - 9) div 18) + 1, ((CurPos.Y - 9) div 18) + 1);
  267.   if not ValidColorIndex(Result.X, Result.Y) then
  268.     Result := Point(-1,-1);
  269. end;
  270.  
  271. procedure TColorArrayEditor.FormCreate(Sender: TObject);
  272. begin
  273.   FLastFrame := Point(-1,-1);
  274. end;
  275.  
  276. procedure TColorArrayEditor.FormMouseMove(Sender: TObject;
  277.   Shift: TShiftState; X, Y: Integer);
  278. begin
  279.   FrameCurrentSquare;
  280. end;
  281.  
  282. procedure TColorArrayEditor.FormShow(Sender: TObject);
  283. begin
  284.   ClientWidth := Colors.XSize * 18 + 18;
  285.   ClientHeight := Colors.YSize * 18 + 42;
  286.   btnOK.Top := Colors.YSize * 18 + 19;
  287.   btnOK.Left := (ClientWidth - btnOK.Width - btnCancel.Width - 5) div 2;
  288.   btnCancel.Top := btnOK.Top;
  289.   btnCancel.Left := btnOK.Left + btnOK.Width + 4;
  290. end;
  291.  
  292. procedure TColorArrayEditor.FormClick(Sender: TObject);
  293. var
  294.   SelectedColorSquare: TPoint;
  295. begin
  296.   SelectedColorSquare := GetCurrentSquare;
  297.   if ValidColorIndex(SelectedColorSquare.X, SelectedColorSquare.Y) then
  298.   begin
  299.     ColorDlg.Color := FColors[SelectedColorSquare.X, SelectedColorSquare.Y];
  300.     if ColorDlg.Execute then
  301.     begin
  302.       with SelectedColorSquare do
  303.       begin
  304.         FColors[X, Y] := ColorDlg.Color;
  305.         DrawSquare(X, Y, ColorDlg.Color, FALSE);
  306.       end;
  307.       FrameCurrentSquare;
  308.     end;
  309.   end;
  310. end;
  311.  
  312. end.
  313.  
  314.